<template>
<div>
  <el-dialog
    :title="title"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="inputForm" ref="inputForm" v-loading="loading" label-width="120px">
      <el-row  :gutter="15">
        <el-col :span="24">
            <el-form-item label="播放日期" prop="dateRange" :rules="[{ type: 'array',required: true, message: '播放日期段不能为空', trigger: 'change' }]">
              <el-date-picker
                v-model="inputForm.dateRange"
                type="daterange"
                align="right"
                unlink-panels
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                :picker-options="pickerOptions"
              ></el-date-picker>
            </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="星期" prop="weekList" :rules="[{type: 'array', required: true, message: '星期不能为空', trigger: 'change' }]">
              <el-checkbox-group v-model="inputForm.weekList">
                <el-checkbox label="星期一"></el-checkbox>
                <el-checkbox label="星期二"></el-checkbox>
                <el-checkbox label="星期三"></el-checkbox>
                <el-checkbox label="星期四"></el-checkbox>
                <el-checkbox label="星期五"></el-checkbox>
                <el-checkbox label="星期六"></el-checkbox>
                <el-checkbox label="星期日"></el-checkbox>
              </el-checkbox-group>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="时间段" prop="timeRange" :rules="[{ type: 'array',required: true, message: '播放时间段不能为空', trigger: 'change' }]">
            <el-time-picker
              is-range
              v-model="inputForm.timeRange"
              range-separator="至"
              start-placeholder="开始时间"
              end-placeholder="结束时间"
              placeholder="选择时间范围">
            </el-time-picker>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">关闭</el-button>
      <el-button type="primary" @click="doSubmit()" v-noMoreClick>确定</el-button>
    </span>
  </el-dialog>
</div>
</template>

<script>
  export default {
    data () {
      return {
        pickerOptions: {
          shortcuts: [
            {
              text: '最近一周',
              onClick (picker) {
                const end = new Date()
                const start = new Date()
                end.setTime(start.getTime() + 3600 * 1000 * 24 * 7)
                picker.$emit('pick', [start, end])
              }
            },
            {
              text: '最近一个月',
              onClick (picker) {
                const end = new Date()
                const start = new Date()
                end.setTime(start.getTime() + 3600 * 1000 * 24 * 30)
                picker.$emit('pick', [start, end])
              }
            },
            {
              text: '最近三个月',
              onClick (picker) {
                const end = new Date()
                const start = new Date()
                end.setTime(start.getTime() + 3600 * 1000 * 24 * 90)
                picker.$emit('pick', [start, end])
              }
            },
            {
              text: '最近一年',
              onClick (picker) {
                const end = new Date()
                const start = new Date()
                end.setTime(start.getTime() + 3600 * 1000 * 24 * 365)
                picker.$emit('pick', [start, end])
              }
            }
          ]
        },
        id: '',
        title: '',
        visible: false,
        loading: false,
        inputForm: {
          dateRange: [new Date(), new Date().setTime((new Date()).getTime() + 3600 * 1000 * 24 * 365 * 2)],
          weekList: ['星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期日'],
          timeRange: [new Date(2020, 1, 1, 0, 0, 0), new Date(2100, 1, 1, 23, 59, 59)]
        }
      }
    },
    components: {
    },
    watch: {

    },
    methods: {
      init (item, id) {
        this.id = id
        this.title = '播放日程'
        this.visible = true
        this.loading = false
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          var worktime = JSON.parse(item.worktime || '{}')
          if (worktime.dateRange) {
            this.inputForm.dateRange = worktime.dateRange
          }
          if (worktime.weekList) {
            this.inputForm.weekList = worktime.weekList
          }
          if (worktime.timeRange) {
            this.inputForm.timeRange = worktime.timeRange
          }
        })
      },
      // 表单提交
      doSubmit () {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            var worktime = JSON.stringify(this.inputForm)
            this.loading = true
            this.$http({
              url: `/info/program/jfInfoProgram/save`,
              method: 'post',
              data: {
                'id': this.id,
                'worktime': worktime
              }
            }).then(({data}) => {
              if (data && data.success) {
                this.visible = false
                this.$message.success(data.msg)
                this.$emit('onDataChange', {
                  'id': this.id,
                  'worktime': worktime
                })
              }
              this.loading = false
            })
          }
        })
      }
    }
  }
</script>
